<?php
require_once(CMF_CONTROLLER_PATH . 'table.php');
if( !isset($config) ){
	$config = array();
}
require_once(APPPATH . 'config/artist_user.php');

class ArtistPassword extends Table{
	var $menu_view = 'includes/artist_menu';
	
	function ArtistPassword(){
		global $config;
		parent::Table($config['artist_user_config']);
		$this->load->model('cmf_artist_password_model');
		$this->model = $this->cmf_artist_password_model;
		$this->flash_messages['save'] = 'Password succesfully updated.';
	}
	
	function index( $sort_field = null, $sort_order = null, $view = CMF_DEFAULT_TABLE_VIEW, $data = array()){
		$this->edit($this->session->userdata('userid'));
	}
	
	function showArchive( $sort_field = null, $sort_order = null, $view = CMF_DEFAULT_TABLE_VIEW, $data = array()){
		redirect(CMF_URL . $this->controller);
	}
	
	function delete($id, $ajax = 0){
		redirect(CMF_URL . $this->controller);
	}
	
	function edit( $id = null, $view = CMF_DEFAULT_TABLE_EDIT_VIEW, $data = array() ){
		$this->_checkUserId($id);
		$data['item'] = $this->model->getItem( $id);
		$data['item']->password = '';
		$data['page_title'] = 'Edit Username and Password';
		$this->getTableAndFieldData($data, $id);
		$this->load->library('form_validation');
		$this->load->helper('form');
		$this->load->view($view, $data);
	}
	
	function editAjax( $id = null, $view = CMF_DEFAULT_TABLE_AJAX_EDIT_VIEW, $data = array()){
		$this->_checkUserId($id);
		parent::editAjax($id, $view, $data);
	}
	
	function editMultiple( $return = null ){
		redirect(CMF_URL . $this->controller);
	}
	
	function setLive($id = null){
		redirect(CMF_URL . $this->controller);
	}
	
	function setNotLive($id = null){
		redirect(CMF_URL . $this->controller);
	}
	
	function archive($id = null){
		redirect(CMF_URL . $this->controller);
	}
	
	function restore($id = null){
		redirect(CMF_URL . $this->controller);
	}
	
	function saveSortOrder(){
		redirect(CMF_URL . $this->controller);
	}
	
	function ajaxItemAccess($id){
		if( is_numeric($id) && $id == $this->session->userdata('userid') ){
			$query = $this->db->get_where( $this->table, array( $this->id_field => $id), 1);
			if( $query && $query->num_rows()){
				$query = $query->result_array();
				return json_encode($query);
			}
		}
		return null;
	}
	
	function save($id = null, $ajax = 0){
		$this->_checkUserId($id);
		if( !$this->input->post('cancel') && $id ){
			if( count($_POST) && isset($_POST['password'])){
				$password = trim($_POST['password']);
				if( empty($password) ){
					unset($_POST['password']);
					unset($_POST['password_confirm']);
					unset($this->model->fields['password']);
					unset($this->model->fields['password_confirmation']);
				}
			}
		}
		parent::save($id, $ajax);
	}
	
	function _checkUserId($id){
		if( $id != $this->session->userdata('userid')){
			redirect($this->params['login_url']);
			return false;
		}
		return true;
	}
}
?>